<template>
  <el-card class="dialog-body" :style="{height:height+'px'}">
    <el-row>
      <el-col :span="14">
        <el-form ref="ref" :model="model" :rules="rules" label-width="120px">
          <el-form-item label="* 系统简称" prop="system_name">
            <el-col :span="8">
              <el-input v-model="model.system_name" type="text" clearable />
            </el-col>
            <el-col :span="16">
              侧边栏、登录页显示，12字以内。
            </el-col>
          </el-form-item>
          <el-form-item label="* 页面标题" prop="page_title">
            <el-col :span="8">
              <el-input v-model="model.page_title" type="text" clearable />
            </el-col>
            <el-col :span="16">
              浏览器页面标题后缀，63字以内。
            </el-col>
          </el-form-item>
          <el-form-item label="favicon" prop="favicon_id">
            <el-col :span="8" style="height:100px">
              <el-image v-if="model.favicon_url" style="height:50px" fit="contain" :src="model.favicon_url" :preview-src-list="[model.favicon_url]" title="点击看大图">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline" />
                </div>
              </el-image>
            </el-col>
            <el-col :span="16">
              <el-button size="mini" @click="fileUpload('favicon', '上传favicon')">上传favicon</el-button>
              <el-button size="mini" @click="fileDelete('favicon')">删除</el-button>
              <p>图片小于 50 KB，jpg、png、ico格式，128 x 128。</p>
            </el-col>
          </el-form-item>
          <el-form-item label="logo" prop="logo_id">
            <el-col :span="8" style="height:100px">
              <el-image v-if="model.logo_url" style="height:100px" fit="contain" :src="model.logo_url" :preview-src-list="[model.logo_url]" title="点击看大图">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline" />
                </div>
              </el-image>
            </el-col>
            <el-col :span="16">
              <el-button size="mini" @click="fileUpload('logo', '上传logo')">上传logo</el-button>
              <el-button size="mini" @click="fileDelete('logo')">删除</el-button>
              <p>图片小于 100 KB，jpg、png格式，200 x 200。</p>
            </el-col>
          </el-form-item>
          <el-form-item label="登录背景图" prop="login_bg_id">
            <el-col :span="8" style="height:100px">
              <el-image v-if="model.login_bg_url" style="height:100px" fit="contain" :src="model.login_bg_url" :preview-src-list="[model.login_bg_url]" title="点击看大图">
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline" />
                </div>
              </el-image>
            </el-col>
            <el-col :span="16">
              <el-button size="mini" @click="fileUpload('login_bg', '上传登录背景')">上传背景图</el-button>
              <el-button size="mini" @click="fileDelete('login_bg')">删除</el-button>
              <p>图片小于 200 KB，jpg、png格式，1920 x 1080。</p>
            </el-col>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" @click="refresh()">刷新</el-button>
            <el-button :loading="loading" type="primary" @click="submit()">提交</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <!-- 文件管理 -->
    <el-dialog :title="fileTitle" :visible.sync="fileDialog" width="80%" top="1vh" :close-on-click-modal="false" :close-on-press-escape="false">
      <file-manage file-type="image" @fileCancel="fileCancel" @fileSubmit="fileSubmit" />
    </el-dialog>
  </el-card>
</template>

<script>
import screenHeight from '@/utils/screen-height'
import FileManage from '@/components/FileManage'
import { systemInfo, systemEdit } from '@/api/system/setting'

export default {
  name: 'SystemSettingSystem',
  components: { FileManage },
  data() {
    return {
      name: '系统设置',
      loading: false,
      model: {
        system_name: '',
        page_title: '',
        favicon_id: 0,
        favicon_url: '',
        logo_id: 0,
        logo_url: '',
        login_bg_id: 0,
        login_bg_url: ''
      },
      fileDialog: false,
      fileField: 'logo',
      fileTitle: '文件管理',
      rules: {}
    }
  },
  created() {
    this.height = screenHeight(180)
    this.info()
  },
  methods: {
    // 信息
    info() {
      systemInfo().then(res => {
        this.model = res.data
        this.$store.dispatch('settings/changeSetting', { key: 'systemName', value: res.data.system_name })
        this.$store.dispatch('settings/changeSetting', { key: 'pageTitle', value: res.data.page_title })
        this.$store.dispatch('settings/changeSetting', { key: 'faviconUrl', value: res.data.favicon_url })
        this.$store.dispatch('settings/changeSetting', { key: 'logoUrl', value: res.data.logo_url })
        this.$forceUpdate()
      })
    },
    // 刷新
    refresh() {
      this.loading = true
      systemInfo().then((res) => {
        this.model = res.data
        this.loading = false
        this.$forceUpdate()
        this.$message.success(res.msg)
      }).catch(() => {
        this.loading = false
      })
    },
    // 提交
    submit() {
      this.$refs['ref'].validate(valid => {
        if (valid) {
          this.loading = true
          systemEdit(this.model).then(res => {
            this.loading = false
            this.$message.success(res.msg)
          }).catch(() => {
            this.loading = false
          })
        }
      })
    },
    // 上传图片
    fileUpload(field, title = '') {
      this.fileField = field
      this.fileTitle = title
      this.fileDialog = true
    },
    fileCancel() {
      this.fileDialog = false
    },
    fileSubmit(fileList) {
      this.fileDialog = false
      const fileField = this.fileField
      const fileLength = fileList.length
      if (fileLength) {
        const i = fileLength - 1
        if (fileField === 'favicon') {
          this.model.favicon_id = fileList[i]['file_id']
          this.model.favicon_url = fileList[i]['file_url']
        } else if (fileField === 'logo') {
          this.model.logo_id = fileList[i]['file_id']
          this.model.logo_url = fileList[i]['file_url']
        } else if (fileField === 'login_bg') {
          this.model.login_bg_id = fileList[i]['file_id']
          this.model.login_bg_url = fileList[i]['file_url']
        }
      }
    },
    fileDelete(field = '') {
      if (field === 'favicon') {
        this.model.favicon_id = 0
        this.model.favicon_url = ''
      } else if (field === 'logo') {
        this.model.logo_id = 0
        this.model.logo_url = ''
      } else if (field === 'login_bg') {
        this.model.login_bg_id = 0
        this.model.login_bg_url = ''
      }
    }
  }
}
</script>

<style scoped>
</style>
